//
//  AppDelegate.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "ViewControllerForGongLue.h"
#import "ViewControllerForReMen.h"
#import "ViewControllerForWoDe.h"
#import "DataBaseHandler.h"
#import "PCH.pch"
#import <UIImageView+WebCache.h>


@interface AppDelegate () <UIScrollViewDelegate>

@property (nonatomic, retain)UITabBarController *tabBarController;

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, retain)NSMutableArray *arrayOfImageView;
@property (nonatomic, retain)UIButton *buttonOfStart;

@property (nonatomic, retain)UIImageView *imageViewForWelcome;

@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [_tabBarController release];
    [_scrollView release];
    [_pageControl release];
    [_arrayOfImageView release];
    [_buttonOfStart release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [self createTabBarController];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    /** 数据库(收藏) */
    [[DataBaseHandler shareDataBaseHandler] open];
    [[DataBaseHandler shareDataBaseHandler] createTable];

    [[DataBaseHandler shareDataBaseHandler] createTable1];
    
    [_window release];
    
    return YES;
}

#pragma mark - UITabBarController
- (void)createTabBarController {
    
    [NSUserDefaults standardUserDefaults];
    
     NSLog(@"%@", NSHomeDirectory());
   
    self.tabBarController = [[UITabBarController alloc] init];
    
    /** item1 */
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:vc];
    [vc release];
    navi1.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"游记" image:[UIImage imageNamed:@"youji"] tag:100] autorelease];
    
    /** item2 */
    ViewControllerForGongLue *vc2 = [[ViewControllerForGongLue alloc] init];
    UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    [vc2 release];
    navi2.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"攻略" image:[UIImage imageNamed:@"gonglve"] tag:200]autorelease];
    
    /** item3 */
    ViewControllerForReMen *vc3 = [[ViewControllerForReMen alloc] init];
    UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    [vc3 release];
    navi3.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"热门" image:[UIImage imageNamed:@"remen"] tag:300]autorelease];
    
    /** item4 */
    ViewControllerForWoDe *vc4 = [[ViewControllerForWoDe alloc] init];
    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    [vc4 release];
    navi4.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"WoDe"] tag:400]autorelease];

    self.tabBarController.viewControllers = @[navi1, navi2, navi3, navi4];
    self.tabBarController.tabBar.dk_barTintColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    self.window.rootViewController = self.tabBarController;
    
#pragma mark - 引导页
    /** 第一次启动 */
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"引导页"]) {
        
        [[NSUserDefaults standardUserDefaults] setBool:1 forKey:@"引导页"];
        
        self.arrayOfImageView = [NSMutableArray array];
        self.arrayOfImageView = @[@"Y1", @"Y2", @"Y3"].mutableCopy;
        
        self.scrollView = [[UIScrollView alloc] init];
        [self.window addSubview:self.scrollView];
        [_scrollView release];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.userInteractionEnabled = YES;
        self.scrollView.frame = self.window.frame;
        self.scrollView.contentSize = CGSizeMake(self.window.frame.size.width * self.arrayOfImageView.count, 0);
        self.scrollView.delegate = self;
        self.scrollView.bounces = NO;
        
        self.pageControl = [[UIPageControl alloc] init];
        [self.window addSubview:self.pageControl];
        [_pageControl release];
        self.pageControl.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
        self.pageControl.center = CGPointMake(self.scrollView.frame.size.width / 2, self.scrollView.frame.size.height - 40);
        self.pageControl.numberOfPages = self.arrayOfImageView.count;
        
        for (int i = 0; i < self.arrayOfImageView.count; i++) {
            
            UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.arrayOfImageView[i]]];
            imageView1.frame = CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
            imageView1.tag = i;
            [self.scrollView addSubview:imageView1];
            imageView1.userInteractionEnabled = YES;
        }
        
        self.buttonOfStart = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImageView *image = [self.window viewWithTag:2];
        [image addSubview:self.buttonOfStart];
        [self.buttonOfStart setTitle:@"开始体验" forState:UIControlStateNormal];
        self.buttonOfStart.layer.borderWidth = 1;
        self.buttonOfStart.layer.borderColor = [[UIColor whiteColor] CGColor];
        self.buttonOfStart.layer.cornerRadius = 7;
        self.buttonOfStart.layer.masksToBounds = YES;
        self.buttonOfStart.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
        self.buttonOfStart.center = CGPointMake(self.window.frame.size.width / 2, self.window.frame.size.height - 80);
        [self.buttonOfStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    [navi1 release];
    [navi2 release];
    [navi3 release];
    [navi4 release];
    
}

- (void)start {
    
    [self.pageControl removeFromSuperview];
    [self.scrollView removeFromSuperview];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger a = self.scrollView.contentOffset.x / self.window.frame.size.width;
    self.pageControl.currentPage = a;
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
